﻿
Imports System.Runtime.CompilerServices
Imports osi.root.constants
Imports osi.root.formation
Imports osi.root.connector
Imports osi.root.utils

Public Module _value_convert
    Private ReadOnly default_array_separators() As Char = {character.comma}

    Friend Function to_string(ByVal s As String) As String
        Return s
    End Function

    <Extension()> Public Function to_bool(ByVal s As String) As Boolean
        If String.IsNullOrEmpty(s) Then
            Return False
        Else
            Dim i As Boolean = False
            If Boolean.TryParse(s, i) Then
                Return i
            Else
                s = s.Trim()
                Return strsame(s, "1", False) OrElse
                       strsame(s, "true", False)
            End If
        End If
    End Function

    <Extension()> Public Function to_int32(ByVal s As String) As Int32
        Dim i As Int32 = 0
        If Int32.TryParse(s, i) Then
            Return i
        Else
            Return 0
        End If
    End Function

    <Extension()> Public Function to_double(ByVal s As String) As Double
        Dim i As Double = 0
        If Double.TryParse(s, i) Then
            Return i
        Else
            Return 0
        End If
    End Function

    <Extension()> Public Function to_uint32(ByVal s As String) As UInt32
        Dim i As UInt32 = 0
        If UInt32.TryParse(s, i) Then
            Return i
        Else
            Return 0
        End If
    End Function

    <Extension()> Public Function to_string_array(ByVal s As String,
                                                  ByVal ParamArray separators() As Char) As vector(Of String)
        If String.IsNullOrEmpty(s) Then
            Return Nothing
        Else
            Dim r As vector(Of String) = Nothing
            r = New vector(Of String)()
            Dim ss() As String = Nothing
            If isemptyarray(separators) Then
                separators = default_array_separators
            End If
            ss = s.Split(separators)
            r.push_back(ss)
            Return r
        End If
    End Function

    Friend Function to_any_array(Of T)(ByVal v As vector(Of String),
                                       ByVal c As Func(Of String, T)) As vector(Of T)
        assert(Not c Is Nothing)
        If v Is Nothing Then
            Return Nothing
        Else
            Dim r As vector(Of T) = Nothing
            r = New vector(Of T)()
            For i As Int32 = 0 To v.size() - 1
                r.push_back(c(v(i)))
            Next
            Return r
        End If
    End Function

    Private Function to_any_array(Of T)(ByVal s As String,
                                        ByVal c As Func(Of String, T)) As vector(Of T)
        Return to_any_array(to_string_array(s), c)
    End Function

    <Extension()> Public Function to_bool_array(ByVal s As String) As vector(Of Boolean)
        Return to_any_array(s, AddressOf to_bool)
    End Function

    <Extension()> Public Function to_int32_array(ByVal s As String) As vector(Of Int32)
        Return to_any_array(s, AddressOf to_int32)
    End Function

    <Extension()> Public Function to_uint32_array(ByVal s As String) As vector(Of UInt32)
        Return to_any_array(s, AddressOf to_uint32)
    End Function
End Module
